package com.hermit.qmst;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.hermit.qmst.UI.activitys.LoginActivity;
import com.hermit.qmst.UI.activitys.MainActivity;
import com.hermit.qmst.request.DownLoadMusic;
import com.hermit.qmst.request.RequestTaskInterface;
import com.hermit.qmst.request.UpdateProcessInterface;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Preferences;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by hermithermit on 15/2/16.
 */
public class MyLauncherActivity extends Activity implements UpdateProcessInterface, RequestTaskInterface {

    private Handler mHandler;
    private String TAG = "MyLauncherActivity";
    private Preferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);

        preferences = new Preferences(this);
        Common.myPhone = preferences.getPreferenceStringValue(Preferences.MY_PHONE);
//        Common.myPhone="13219963757";
        Common.myPassword = preferences.getPreferenceStringValue(Preferences.MY_PASSWORD);
        Common.mAgentId = preferences.getPreferenceStringValue(Preferences.SIP_AGENT);
        Common.mAgentName = this.getString(R.string.app_name);
//        Common.mAgentName="测试专用";
        mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (Common.myPhone != null && Common.myPhone.length() > 0) {
                    getCallTone();
                    Intent intent = new Intent(MyLauncherActivity.this, MainActivity.class);
                    startActivity(intent);
                    MyLauncherActivity.this.finish();
                } else {
                    getLoginSet();
                }
            }
        }, 1000L);
        DisplayImageOptions defaultOptions = new DisplayImageOptions
                .Builder()
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration
                .Builder(getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .discCacheSize(50 * 1024 * 1024)//
                .discCacheFileCount(100)//缓存一百张图片
                .writeDebugLogs()
                .build();
        ImageLoader.getInstance().init(config);

    }

    private void getCallTone() {
        String AGENT_THIRD = preferences.getPreferenceStringValue(Preferences.SIP_AGENT_THIRD);
        if (AGENT_THIRD.equals("0")) {
            AGENT_THIRD = Common.mAgentId;
        }
        DownLoadMusic downLoadMusic = new DownLoadMusic(this, Common.getMusic() + AGENT_THIRD, this, this);
        downLoadMusic.execute();
    }

    /**
     * 登录设置
     */
    private void getLoginSet() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = "";
        try {
            url = Common.mUrl + "szdh/loginState.php?type=isShowReg&acct=" + URLEncoder.encode(Common.mAgentName, "utf-8");
        } catch (UnsupportedEncodingException e) {
        }
        Log.i(TAG, url);
        JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject object) {
                Log.i(TAG, object.toString());
                try {
                    JSONObject data = object.getJSONObject("data");
                    if (data.getInt("state") == 0) {
                        Common.isShowRegister = false;
                    } else {
                        Common.isShowRegister = true;
                    }
                    if (data.getInt("mstate") == 0) {
                        Common.isShowSmsVerification = false;
                    } else {
                        Common.isShowSmsVerification = true;
                    }

                } catch (JSONException e) {

                } finally {
                    preferences.setPreferenceBooleanValue("isShowSmsVerification", Common.isShowSmsVerification);
                    preferences.setPreferenceBooleanValue("isShowRegister", Common.isShowRegister);
                    Intent intent = new Intent(MyLauncherActivity.this, LoginActivity.class);
                    startActivity(intent);
                    MyLauncherActivity.this.finish();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                preferences.setPreferenceBooleanValue("isShowSmsVerification", Common.isShowSmsVerification);
                preferences.setPreferenceBooleanValue("isShowRegister", Common.isShowRegister);
                Intent intent = new Intent(MyLauncherActivity.this, LoginActivity.class);
                startActivity(intent);
                MyLauncherActivity.this.finish();
            }
        });
        requestQueue.add(request);
    }

    @Override
    public void postExecute(String paramString) {
        Log.e("ss", paramString);
    }

    @Override
    public void processUpate(int value) {
//        mProgressBar.setProgress(value);
    }
}
